package com.easy.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.ConcertBean;
import com.easy.common.bean.OrderBean;
import com.easy.common.bean.UserBean;
import com.easy.common.service.IOrderService;
import com.easy.common.service.ISeatService;
import com.easy.common.service.IUserService;
import com.easy.common.util.Result;
import com.easy.order.service.OrderServiceImpl;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RequestMapping("/order")
@RestController
@CrossOrigin
public class OrderController {
    @Autowired
    IOrderService orderService;

    //新增订单
    @PostMapping("/add")
    public Result addOrder(@RequestBody OrderBean order) {
        Object order_data = orderService.addOrder(order);
        return Result.success(order_data);
    }

    //软删除订单
    @DeleteMapping("/delete")
    public Result deleteOrder(@PathVariable("id") Integer id) {
        Object order_data = orderService.deleteOrder(id);
        return Result.success(order_data);
    }

    //edit修改用户信息
    @PutMapping("/edit")
    public Result uploadOrder(@RequestBody OrderBean order) {
        Object order_data = orderService.uploadOrder(order);
        return Result.success(order_data);
    }


    //查询所有订单信息
    @GetMapping("/all")
    public Result getAll(OrderBean order) {
        Object order_data = orderService.getAll(order);
        return Result.success(order_data);
    }

    //分页查询订单信息
    @GetMapping("/page")
    public Result getPage(OrderBean order, Page<OrderBean> page) {
        IPage<OrderBean> orderWithUserNickname = orderService.getOrderWithUserNickname(page);
        return Result.success(orderWithUserNickname);
//        orderService.getPage(order, page);
//        return Result.success(page);
    }

    @GetMapping("/getnickname")
    public Result getOrderWithUserNickname(Page<OrderBean> page) {
        IPage<OrderBean> orderWithUserNickname = orderService.getOrderWithUserNickname(page);
        return Result.success(orderWithUserNickname);
    }
    // 新增：根据用户ID查询订单（带演唱会信息）
    @GetMapping("/user/{userId}")
    public Result getOrdersByUserId(@PathVariable Integer userId, Page<OrderBean> page) {
        IPage<OrderBean> orders = orderService.getOrdersByUserIdWithConcertInfo(userId, page);
        return Result.success(orders);
    }
    @GetMapping("/seatPrice/{seatPriceId}")
    public Result getOrdersBySeatPriceId(@PathVariable Integer seatPriceId, Page<OrderBean> page) {
        IPage<OrderBean> orders = orderService.getOrdersByMultiCondition(seatPriceId, null, null, null, page);
        return Result.success(orders);
    }

    // 新增：多条件查询订单（带演唱会信息）
    @GetMapping("/multiCondition")
    public Result getOrdersByMultiCondition(
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Long startDate,
            @RequestParam(required = false) Long endDate,
            Page<OrderBean> page) {

        Date start = startDate != null ? new Date(startDate) : null;
        Date end = endDate != null ? new Date(endDate) : null;

        IPage<OrderBean> orders = orderService.getOrdersByMultiCondition(userId, status, start, end, page);
        return Result.success(orders);
    }
}